package shiqi.chain.version2;

import org.springframework.stereotype.Component;
import shiqi.chain.code.ContextHolder;
import shiqi.chain.code.ContextRequest;
import shiqi.chain.code.ContextResponse;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shiqi
 * @version 1.0.0
 * @ClassName OrderService.java
 * @Description
 * @createTime 2022-05-20
 */
@Component
public class OrderService implements Flow<ContextRequest, ContextResponse>  {

    @Resource
    private List<Stage<ContextRequest, ContextResponse, ContextHolder>> stageList;

    @Override
    public ContextResponse execute(ContextRequest input) {
        Pipeline<ContextRequest, ContextResponse, ContextHolder> pipeline = new Pipeline<>();
        pipeline.setRequest(input);
        pipeline.setContext(new ContextHolder());
        pipeline.setResponse(new ContextResponse());

        for (Stage<ContextRequest, ContextResponse, ContextHolder> stage : stageList) {
            stage.process(pipeline);
        }

        return pipeline.getResponse();
    }
}
